import 'package:day_demo/src/mt_scaffold/mt_scaffold.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:sprung/sprung.dart';

class KeyboardSensitiveView extends StatelessWidget {
  const KeyboardSensitiveView({super.key});

  @override
  Widget build(BuildContext context) {
    final mq = MediaQuery.of(context);
    final bottomOffset = mq.viewInsets.bottom + mq.padding.bottom;
    return KeyboardVisibilityBuilder(
      builder: (ctx, visible) {
        var curve = Curves.fastLinearToSlowEaseIn;
        if (!visible) curve = Curves.easeOutQuad;
        var mills = 110;
        if (visible) mills = 100;

        return KeyboardDismissOnTap(
          child: Scaffold(
            resizeToAvoidBottomInset: false,
            backgroundColor: Colors.white,
            appBar: AppBar(),
            body: ListView(
              children: List.generate(
                  20,
                  (index) => TextComposer(
                        key: Key(index.toString()),
                      )).toList(),
            ),
            // body: Scaffold(
            //   body: ListView(
            //     children: List.generate(
            //         20,
            //         (index) => TextComposer(
            //               key: Key(index.toString()),
            //             )).toList(),
            //   ),
            //   persistentFooterButtons: const [
            //     Text('data'),
            //   ],
            // ),
            bottomNavigationBar: AnimatedContainer(
              curve: Sprung.overDamped,
              duration: const Duration(
                milliseconds: 50,
              ),
              padding: EdgeInsets.only(
                bottom: bottomOffset,
              ),
              color: Colors.white,
              child: SafeArea(
                bottom: false,
                maintainBottomViewPadding: true,
                child: SizedBox(
                  height: 50,
                  child: Container(
                    color: Colors.white,
                    child: Row(
                      children: [
                        Expanded(
                            child: TextButton(
                                onPressed: () {}, child: const Text('取消'))),
                        Expanded(
                            child: TextButton(
                                onPressed: () {}, child: const Text('保存'))),
                      ],
                    ),
                  ),
                ),
              ),
            ),
            // persistentFooterButtons: [
            //   AnimatedContainer(
            //     // curve: curve,
            //     duration: const Duration(
            //       milliseconds: 0,
            //     ),
            //     padding: EdgeInsets.only(
            //       bottom: bottomOffset,
            //     ),
            //     color: Colors.white,
            //     child: SafeArea(
            //       bottom: false,
            //       top: false,
            //       maintainBottomViewPadding: true,
            //       child: SizedBox(
            //         // height: 20,
            //         child: Container(
            //           color: Colors.blueAccent,
            //           child: Row(
            //             children: [
            //               Expanded(
            //                   child: TextButton(
            //                       onPressed: () {}, child: const Text('取消'))),
            //               Expanded(
            //                   child: TextButton(
            //                       onPressed: () {}, child: const Text('保存'))),
            //             ],
            //           ),
            //         ),
            //       ),
            //     ),
            //   ),
            // ],
            // bottomSheet: AnimatedContainer(
            //   // curve: curve,
            //   duration: const Duration(
            //     milliseconds: 0,
            //   ),
            //   padding: EdgeInsets.only(
            //     bottom: bottomOffset,
            //   ),
            //   color: Colors.white,
            //   child: SafeArea(
            //     bottom: false,
            //     maintainBottomViewPadding: true,
            //     child: SizedBox(
            //       height: 50,
            //       child: Container(
            //         color: Colors.white,
            //         child: Row(
            //           children: [
            //             Expanded(
            //                 child: TextButton(
            //                     onPressed: () {}, child: const Text('取消'))),
            //             Expanded(
            //                 child: TextButton(
            //                     onPressed: () {}, child: const Text('保存'))),
            //           ],
            //         ),
            //       ),
            //     ),
            //   ),
            // ),
          ),
        );
      },
    );
  }
}

class TextComposer extends StatefulWidget {
  const TextComposer({
    super.key,
    this.color,
  });
  final Color? color;
  @override
  State<TextComposer> createState() => _TextComposerState();
}

class _TextComposerState extends State<TextComposer> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.color ?? Colors.white,
      padding: const EdgeInsets.symmetric(vertical: 6),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          const SizedBox(
            width: 60,
            child: Icon(Icons.add_a_photo),
          ),
          Flexible(
            child: TextField(
              style: Theme.of(context).textTheme.titleMedium,
              maxLines: 4,
              minLines: 1,
              decoration: const InputDecoration(
                  border: InputBorder.none, hintText: 'Enter text...'),
            ),
          ),
          const SizedBox(
            width: 60,
            child: Icon(Icons.send),
          ),
        ],
      ),
    );
  }
}

class KeyboardAnimationExample extends StatefulWidget {
  const KeyboardAnimationExample({super.key});

  @override
  _KeyboardAnimationExampleState createState() =>
      _KeyboardAnimationExampleState();
}

class _KeyboardAnimationExampleState extends State<KeyboardAnimationExample>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _heightAnimation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: Duration(milliseconds: 300),
      vsync: this,
    );
    _heightAnimation = Tween<double>(begin: 0, end: 250).animate(
      CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  void showKeyboard() {
    _controller.forward();
  }

  void hideKeyboard() {
    _controller.reverse();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('键盘动画示例'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: showKeyboard,
              child: Text('显示键盘'),
            ),
            ElevatedButton(
              onPressed: hideKeyboard,
              child: Text('隐藏键盘'),
            ),
            AnimatedBuilder(
              animation: _heightAnimation,
              builder: (context, child) {
                return Container(
                  height: _heightAnimation.value,
                  width: MediaQuery.of(context).size.width,
                  color: Colors.grey,
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}
